package cn.sykj.car.controller;


import cn.sykj.car.common.ResponseBean;
import cn.sykj.car.common.StatusCode;
import cn.sykj.car.dict.DeleteFlagEnum;
import cn.sykj.car.entity.Region;
import cn.sykj.car.service.IRegionService;
import cn.sykj.car.utils.Utils;
import cn.sykj.car.vo.RegionVO;
import cn.sykj.car.vo.ResponseMessageVO;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 中国地区信息 前端控制器
 * </p>
 *
 * @author yixu
 * @since 2020-06-18
 */
@RestController
@RequestMapping("/region")
@Slf4j
public class RegionController extends BaseController {

    @Resource
    IRegionService iRegionService;




    /**
     * 查询未设置配送的地区
     * @param id 模板id
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "查询未设置配送的地区", notes = "查询未设置配送的地区")
    @ApiImplicitParams({

    })
    @RequiresAuthentication
    @RequestMapping(value = "/{id}/listNotExists", method = RequestMethod.POST)
    public cn.sykj.car.common.ResponseBean listNotExists(@PathVariable Long id) throws Exception{

        // 获取登录用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String)obj);

        List<RegionVO> regionVOs = iRegionService.listNotExists(id);
        return new cn.sykj.car.common.ResponseBean(cn.sykj.car.common.StatusCode.OK,regionVOs);
    }

    /**
     * 根据配送区域配置freight_model_detail.id 获取已选中的配送区域
     * @param modelDetailId 模板明细id
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "根据配送区域配置freight_model_detail.id 获取已选中的配送区域",
            notes = "根据配送区域配置freight_model_detail.id 获取已选中的配送区域")
    @ApiImplicitParams({

    })
    @RequiresAuthentication
    @RequestMapping(value = "/{modelDetailId}/listExists", method = RequestMethod.POST)
    public cn.sykj.car.common.ResponseBean listExists(@PathVariable Long modelDetailId) throws Exception{

        // 获取登录用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String)obj);

        List<RegionVO> regionVOs = iRegionService.listExists(modelDetailId);
        return new cn.sykj.car.common.ResponseBean(cn.sykj.car.common.StatusCode.OK,regionVOs);
    }


    /**
     * 查询全部地区
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "查询全部地区", notes = "查询全部地区")
    @ApiImplicitParams({

    })
    @RequiresAuthentication
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public cn.sykj.car.common.ResponseBean list() throws Exception{

        // 获取登录用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String)obj);

        List<RegionVO> regionVOs = iRegionService.all();
        return new cn.sykj.car.common.ResponseBean(cn.sykj.car.common.StatusCode.OK,regionVOs);
    }

    /**
     * 查询全部地区
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "查询全部地区", notes = "查询全部地区")
    @ApiImplicitParams({

    })
    @RequiresAuthentication
    @RequestMapping(value = "/listL3", method = RequestMethod.POST)
    public cn.sykj.car.common.ResponseBean listL3() throws Exception{

        // 获取登录用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String)obj);

        List<RegionVO> regionVOs = iRegionService.listL3();
        return new cn.sykj.car.common.ResponseBean(cn.sykj.car.common.StatusCode.OK,regionVOs);
    }

    /**
     * 添加地区
     * @param region
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "添加地区", notes = "添加地区")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("region:create")
    @RequestMapping(value = "/addRegion", method = RequestMethod.POST)
    public ResponseBean addRegion(@RequestBody Region region) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        region.setCreateTime(new Date());
        region.setUpdateTime(new Date());
        region.setDelFlag(DeleteFlagEnum.FALSE.VALUE);
        region.setTenentCode("00000000");

        ResponseMessageVO responseMessageVO = iRegionService.addRegion(region);
        if (Utils.notEmpty(responseMessageVO) && responseMessageVO.isRetFlag()) {
            this.writeLog2Manager(responseMessageVO);
        }
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }

    /**
     * 删除地区
     *
     * @param id
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "删除地区", notes = "删除地区")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("region:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public ResponseBean delete(@PathVariable Integer id) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);


        ResponseMessageVO responseMessageVO = iRegionService.delete(id);
        if (Utils.notEmpty(responseMessageVO) && responseMessageVO.isRetFlag()) {
            this.writeLog2Manager(responseMessageVO);
        }
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }

    /**
     * 修改地区
     * @param region
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "修改地区", notes = "修改地区")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("region:edit")
    @RequestMapping(value = "/updateRegion", method = RequestMethod.POST)
    public ResponseBean updateRegion(@RequestBody Region region) throws Exception{

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        region.setUpdateTime(new Date());
        ResponseMessageVO responseMessageVO=iRegionService.updateRegion(region);
        this.writeLog2Manager(responseMessageVO);
        return new ResponseBean(StatusCode.OK,responseMessageVO);
    }

}
